<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/7
 * Time: 7:55
 */

namespace Admin\Controller;

use Think\Page;

class ServerController extends AdminController
{
    public function index()
    {
        $p = I('p', '1');
        $page_size = 10;
        $servers = M('Server');
        $count = $servers->count();
        $list = $servers->page($p . ',' . $page_size)->select();
        $page = new Page($count, $page_size);
        $this->assign('list', $list);
        $this->assign('page', $page->show());
        $this->display();
    }

    public function add()
    {
        $this->display();
    }

    public function edit()
    {
        $id = I('id', '');
        if (!empty($id)) {
            $where = array('id' => $id);
            $server = M('Server')->where($where)->select();
            if ($server) {
                $this->assign('server', $server);
                $this->display();
            }
            $this->error('查找失败');
        } else {
            $this->error('查找失败');
        }
    }

    public function update()
    {
        $id = I('id', '');
        $name = I('name', '');
        $qq_number = I('qq_number', '');

        $data['name'] = $name;
        $data['qq_number'] = $qq_number;

        $server = M('Server');

        if (empty($id)) {
            $res = $server->data($data)->add();
            if ($res) {
                $this->success('添加成功!');
            } else {
                $this->error('添加失败!');
            }
        } else {
            $data['id'] = $id;
            $count = $server->where(array('id' => $id))->count();
            if ($count) {
                $res = $server->data($data)->save();
                if ($res) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败!');
                }
            } else {
                $this->error('更新失败!');
            }
        }
    }

    public function delete()
    {
        $id = I('id', '');
        if (!empty($id)) {
            $res = M('Server')->where(array('id' => $id))->delete();
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        }else{
            $this->error("删除失败");
        }
    }

}